<template>
  <div class="access-page">
    <h4 class="zl-title f-mt0">访问权列表
      <div class="fr">
        <search placeholder="请输入名称/域名" @change="searchAccess"></search>
        <Button type="primary" @click="accessAddModify()" class="fr" v-if="hasPermission($store, $route.params.environmentId, 'INGRESS_POST_ENV')">
          <Icon type="md-add" /> 添加访问权
        </Button>
      </div>
    </h4>
    <div class="zl-page-loading" v-if="isLoading">
      <Spin fix></Spin>
    </div>
    <Table :columns="columns" :data="accessList" class="access-table" v-else></Table>

    <!-- 新增、修改 -->
    <add-modify-model :visible.sync="accessVisible" :form="accessForm" @finish="init" ></add-modify-model>
    <!-- 详情 -->
    <!-- <detail-model :visible.sync="detailVisible" :form="accessForm"></detail-model> -->
  </div>
</template>
<script type="text/javascript">
import Fetch from '@/api/request'
import AddModifyModel from '@/components/access-right/add-modify-model'
import Search from '@/components/search'

export default {
  components: { AddModifyModel, Search },
  data() {
    return {
      columns: [{
        type: 'index',
        width: 60
      }, {
        title: '名称',
        render: (h, params) => {
          return h('a', {
            class: 'zl-link',
            on: {
              click: () => {
                this.accessDetail(params.row)
              }
            }
          }, params.row.id)
        }
      }, {
        title: '域名',
        render: (h, params) => {
          return h('a', {
            attrs: {
              'target': '_blank',
              'href': 'http://' + params.row.host
            }
          }, params.row.host)
        }
      }, {
        title: '创建时间',
        key: 'creationTimestamp'
      }, {
        title: '操作',
        render: (h, params) => {
          let btnList = []
          if (this.hasPermission(this.$store, this.$route.params.environmentId, 'INGRESS_PUT_ENV|INGRESS')) {
            btnList.push(h('div', {
              class: 'button-inner',
              on: {
                click: () => {
                  this.accessAddModify(params.row)
                }
              }
            }, [
              h('Icon', {
                props: {
                  type: 'ios-create-outline'
                }
              }, ''),
              '编辑'
            ]))
          }
          if (this.hasPermission(this.$store, this.$route.params.environmentId, 'INGRESS_DELETE_ENV|INGRESS')) {
            btnList.push(h('div', {
              class: 'button-inner',
              on: {
                click: () => {
                  this.accessDelete(params.row)
                }
              }
            }, [
              h('Icon', {
                props: {
                  type: 'md-trash'
                }
              }, ''),
              '删除'
            ]))
          }

          return h('div', btnList)
        }
      }],
      accessList: [],
      accessListOrigin: [],
      accessForm: {},
      accessVisible: false,
      detailVisible: false,
      isLoading: true
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 根据名称搜索
    searchAccess(name) {
      let temp = []
      if (name) {
        this.accessListOrigin.forEach((access) => {
          if (access.id.indexOf(name) !== -1 || access.host.indexOf(name) !== -1) {
            temp.push(access)
          }
        })
      } else {
        temp = this.accessListOrigin
      }
      this.accessList = temp
    },
    // 删除访问权
    accessDelete(access) {
      this.$Modal.confirm({
        title: '系统提示',
        content: '确定删除访问权（' + access.id + '）?',
        loading: true,
        onOk: () => {
          Fetch.delete({
            url: '/api/environments/' + this.$route.params.environmentId + '/ingresses/' + access.id,
            done: () => {
              this.$Modal.remove()
            }
          }).then((res) => {
            this.$Message.success({
              content: '删除访问权（' + access.id + '）成功！',
              duration: this.DURATION
            })
            this.init()
          })
        }
      })
    },
    // 访问权详情
    accessDetail(access) {
      this.$router.push('/home/environment/' + this.$route.params.environmentId + '/access-right/' + access.id)
    },
    // 访问权新增、修改
    accessAddModify(access) {
      if (access) {
        let d = access.host.split('.')
        let t = ''
        if (d.length > 3) {
          for (let i = 0; i < d.length - 3; i++) {
            const item = d[i]
            if (i === 0) {
              t += item
            } else {
              t += '.' + item
            }
          }
        } else if (d.length === 3) {
          t = d[0]
        }
        this.accessForm = Object.assign({environmentName: access.environmentId, level3domen: t}, access)
      } else {
        this.accessForm = {
          environmentName: '',
          id: '',
          host: '.' + localStorage.getItem('secondaryDomain') + '.' + localStorage.getItem('domain'),
          maxBodySize: '8',
          level3domen: '',
          paths: []
        }
      }
      this.accessVisible = true
    },
    // 获取访问权初始数据
    init() {
      Fetch.get({
        url: '/api/environments/' + this.$route.params.environmentId + '/ingresses',
        done: () => {
          this.isLoading = false
        }
      }).then((res) => {
        this.accessList = res.data
        this.accessListOrigin = res.data
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.access-page {
  .access-table {
    margin-top: 20px;
  }
  .zl-title {
    margin-top: 10px;
  }
}
</style>
